﻿namespace Vdc.Grid
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class GridColumnsFooter : WebControl, INamingContainer
    {
        private Vdc.Grid.Grid grid;
        private HtmlContainer innerContainer;

        public GridColumnsFooter(Vdc.Grid.Grid _grid)
        {
            this.grid = _grid;
            this.ID = this.GetColumnsFooterID();
        }

        protected virtual GridDataControlFieldCell CreateCell(Column field)
        {
            GridDataControlFieldCell cell = new GridDataControlFieldCell(field);
            if (field.Visible)
            {
                cell.CssClass = !field.Wrap ? "ob_gC" : "ob_gCW";
            }
            else
            {
                cell.CssClass = !field.Wrap ? "ob_gC_I" : "ob_gCW_I";
            }
            if (field.Index == this.grid.IndexOfFirstVisibleColumn)
            {
                cell.CssClass = cell.CssClass + " ob_gC_Fc";
            }
            else if (field.Index == this.grid.IndexOfLastVisibleColumn)
            {
                cell.CssClass = cell.CssClass + " ob_gC_Lc";
            }
            if (this.grid.IsDesignTimeRender && this.grid.UseEmbeddedImages)
            {
                cell.Style["background-image"] = "url(" + this.grid.ImagesURL.ColumnsFooter + ")";
            }
            if (field.Width.IndexOf("%") == -1)
            {
                cell.Width = Unit.Pixel(int.Parse(field.Width));
            }
            else
            {
                cell.Width = Unit.Percentage((double) int.Parse(field.Width.Replace("%", "")));
            }
            if (this.grid.IsDesignTimeRender)
            {
                if (!field.Visible)
                {
                    cell.Style["display"] = "none";
                    cell.Width = Unit.Pixel(0);
                }
                cell.Attributes["_designerRegion"] = field.Index.ToString();
            }
            HtmlContainer child = new HtmlContainer();
            HtmlContainer container2 = new HtmlContainer();
            child.CssClass = "ob_gCc1";
            if (string.IsNullOrEmpty(field.Align) || (field.Align.ToLower() == "left"))
            {
                container2.CssClass = "ob_gCc2";
            }
            else if (field.Align.ToLower() == "center")
            {
                container2.CssClass = "ob_gCc2C";
            }
            else
            {
                container2.CssClass = "ob_gCc2R";
            }
            child.Controls.Add(container2);
            cell.Controls.Add(child);
            return cell;
        }

        protected virtual void CreateCellSeparator(string width, bool visible)
        {
            HtmlContainer child = new HtmlContainer {
                Text = "&#160;",
                CssClass = "ob_gCS"
            };
            child.Style["left"] = width;
            if (!visible || this.grid.HasColumnsWithFixedAndPercentageWidth)
            {
                child.Style["display"] = "none";
            }
            this.innerContainer.Controls.Add(child);
        }

        protected override void CreateChildControls()
        {
            this.CssClass = "ob_gCFCont";
            this.innerContainer = new HtmlContainer();
            this.innerContainer.CssClass = "ob_gCFICont";
            Table child = new Table {
                CssClass = this.grid.CssSettings.CSSColumnsFooter
            };
            child.Attributes["cellspacing"] = "0";
            child.Attributes["cellpadding"] = "0";
            child.Width = Unit.Percentage(100.0);
            this.innerContainer.Controls.Add(child);
            this.Controls.Add(this.innerContainer);
            GridRow row = new GridRow(0, 0, GridRowType.ColumnFooter, DataControlRowState.Normal);
            child.Controls.Add(row);
            int num = 0;
            int num2 = 0;
            foreach (Column column in this.grid.Columns)
            {
                row.Controls.Add(this.CreateCell(column));
                if (num < (this.grid.Columns.Count - 1))
                {
                    if (column.Width.IndexOf("%") == -1)
                    {
                        this.CreateCellSeparator(((num2 + int.Parse(column.Width))).ToString() + "px", column.Visible);
                    }
                    else
                    {
                        this.CreateCellSeparator(((num2 + int.Parse(column.Width.Replace("%", "")))).ToString() + "%", column.Visible);
                    }
                }
                if (column.Visible)
                {
                    num2 += int.Parse(column.Width.Replace("%", ""));
                }
                num++;
            }
            GridRowEventArgs e = new GridRowEventArgs(row);
            this.grid.OnRowCreated(e);
            this.grid.OnRowDataBound(e);
        }

        internal virtual void Export(Html32TextWriter oHtmlTextWriter)
        {
            this.EnsureChildControls();
            TableRow row = this.innerContainer.Controls[0].Controls[0] as TableRow;
            TableRow child = new TableRow();
            foreach (int num in this.grid.columnIndexesToExport)
            {
                TableCell cell = new TableCell();
                if (this.grid.ExportingSettings.KeepColumnSettings)
                {
                    cell.Attributes["width"] = this.grid.Columns[num].Width;
                    cell.Attributes["align"] = this.grid.Columns[num].HeaderAlign;
                    if (!this.grid.Columns[num].Wrap)
                    {
                        cell.Attributes["nowrap"] = "nowrap";
                    }
                }
                cell.Text = ((HtmlContainer) row.Controls[num].Controls[0].Controls[0]).Text;
                if (this.grid.Columns[num].ExportAsText)
                {
                    cell.Text = "&nbsp;" + cell.Text;
                }
                child.Controls.Add(cell);
            }
            this.grid.exportedGrid.Controls.Add(child);
            this.grid.OnRowExported(child, this.innerContainer.Controls[0].Controls[0] as GridRow);
        }

        internal void ForceEnsureChildControls()
        {
            this.EnsureChildControls();
        }

        protected virtual string GetColumnsFooterID()
        {
            return ("ob_" + this.grid.GridID + "ColumnsFooterContainer");
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
    }
}

